import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { PerEmployeeApi } from '#/api/per/employee';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { formatDate, handleTree } from '@vben/utils';

import { getDeptListCondition } from '#/api/system/dept';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'photoUrl',
      label: '头像',
      component: 'ImageUpload',
      formItemClass: 'col-span-2',
      componentProps: {
        maxNumber: 1,
        params: {
          fileCategoriesCode: 'GRZH',
          objectId: 'GRTX',
          objectType: '个人头像',
        },
      },
    },
    {
      fieldName: 'fullName',
      label: '姓名',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入姓名',
      },
    },
    {
      fieldName: 'mobile',
      label: '联系电话',
      component: 'Input',
      rules: 'required',
      componentProps: {
        placeholder: '请输入联系电话',
      },
    },
    {
      fieldName: 'cardType',
      label: '证件类型',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.SYSTEM_CARD_TYPE, 'string'),
        placeholder: '请选择证件类型',
      },
    },
    {
      fieldName: 'idcard',
      label: '证件号码',
      component: 'Input',
      rules: 'required',
      componentProps: {
        placeholder: '请输入身份证号',
      },
    },
    {
      fieldName: 'formerName',
      label: '曾用名',
      component: 'Input',
      componentProps: {
        placeholder: '请输入曾用名',
      },
    },

    {
      fieldName: 'ethnicity',
      label: '民族',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.SYSTEM_NATION, 'string'),
        placeholder: '请输入民族',
      },
    },

    {
      fieldName: 'nativePlace',
      label: '籍贯',
      component: 'Input',
      componentProps: {
        placeholder: '请输入籍贯',
      },
    },
    {
      fieldName: 'birthPlace',
      label: '出生地',
      component: 'Input',
      componentProps: {
        placeholder: '请输入出生地',
      },
    },
    {
      fieldName: 'politicalStatus',
      label: '政治面貌',
      component: 'Select',
      rules: 'required',
      componentProps: {
        options: getDictOptions(DICT_TYPE.SYSTEM_POLITICALINFO, 'string'),
        placeholder: '请选择政治面貌',
      },
    },
    {
      fieldName: 'partyJoinDate',
      label: '入党时间',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'x',
      },
    },
    {
      fieldName: 'householdAddress',
      label: '户籍地址',
      component: 'Input',
      componentProps: {
        placeholder: '请输入户籍地址',
      },
    },
    {
      fieldName: 'currentAddress',
      label: '常住地址',
      component: 'Input',
      componentProps: {
        placeholder: '请输入常住地址',
      },
    },
    {
      fieldName: 'homeAddress',
      label: '家庭地址',
      component: 'Input',
      componentProps: {
        placeholder: '请输入家庭地址',
      },
    },

    {
      fieldName: 'unitId',
      label: '单位',
      component: 'ApiTreeSelect',
      rules: 'required',
      componentProps: {
        api: async () => {
          const data = await getDeptListCondition({ type: 'unit' });
          return handleTree(data);
        },
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        placeholder: '请选择单位',
        treeDefaultExpandAll: true,
        allowClear: true,
      },
    },

    {
      fieldName: 'staffType',
      label: '编制类型',
      component: 'Select',
      rules: 'required',
      componentProps: {
        options: getDictOptions(DICT_TYPE.PER_STAFF_TYPE, 'string'),
        placeholder: '请选择编制类型',
      },
    },

    {
      fieldName: 'teacherCertificate',
      label: '教师资格证号',
      component: 'Input',
      rules: 'required',
      componentProps: {
        placeholder: '请输入教师资格证号',
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '系统人员ID，对应 system_person.id',
      field: 'systemPersonId',
    },
    {
      label: '姓名',
      field: 'fullName',
    },
    {
      label: '证件类型',
      field: 'cardTypeName',
    },
    {
      label: '身份证号',
      field: 'idcard',
    },
    {
      label: '曾用名',
      field: 'formerName',
    },
    {
      label: '出生年月',
      field: 'birthDate',
      render: (val) => formatDate(val, 'YYYY-MM-DD') as string,
    },
    {
      label: '参加工作时间',
      field: 'employmentDate',
      render: (val) => formatDate(val, 'YYYY-MM-DD') as string,
    },
    {
      label: '性别',
      field: 'sexName',
    },
    {
      label: '状态',
      field: 'statusName',
    },
    {
      label: '入党时间',
      field: 'partyJoinDate',
      render: (val) => formatDate(val, 'YYYY-MM-DD') as string,
    },
    {
      label: '民族',
      field: 'ethnicity',
    },
    {
      label: '毕业时间',
      field: 'graduationDate',
      render: (val) => formatDate(val, 'YYYY-MM-DD') as string,
    },
    {
      label: '籍贯',
      field: 'nativePlace',
    },
    {
      label: '其他参数',
      field: 'metadata',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
    {
      label: '出生地',
      field: 'birthPlace',
    },
    {
      label: '联系电话',
      field: 'mobile',
    },
    {
      label: '从教起始时间',
      field: 'teachingStartDate',
      render: (val) => formatDate(val, 'YYYY-MM-DD') as string,
    },
    {
      label: '户籍地址',
      field: 'householdAddress',
    },
    {
      label: '常住地址',
      field: 'currentAddress',
    },
    {
      label: '家庭地址',
      field: 'homeAddress',
    },
    {
      label: '身份类别',
      field: 'identityTypeName',
    },
    {
      label: '岗位',
      field: 'postId',
    },
    {
      label: '政治面貌',
      field: 'politicalStatusName',
    },
    {
      label: '最高学历',
      field: 'highestEducation',
    },
    {
      label: '毕业院校',
      field: 'graduationSchool',
    },
    {
      label: '专业',
      field: 'major',
    },
    {
      label: '档案号',
      field: 'fileNumber',
    },
    {
      label: '照片',
      field: 'photoUrl',
    },
    {
      label: '单位id',
      field: 'unitId',
    },
    {
      label: '部门ID',
      field: 'deptId',
    },
    {
      label: '行政区划编码',
      field: 'areaCode',
    },
    {
      label: '备注',
      field: 'remarks',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
    {
      label: '编制类型',
      field: 'staffTypeName',
    },
    {
      label: '任教学科',
      field: 'teachingSubject',
    },
    {
      label: '教师资格证',
      field: 'teacherCertificate',
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'fullName',
      label: '姓名',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入姓名',
      },
    },
    {
      fieldName: 'cardType',
      label: '证件类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.SYSTEM_CARD_TYPE, 'string'),
        placeholder: '请选择证件类型',
      },
    },
    {
      fieldName: 'idcard',
      label: '身份证号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入身份证号',
      },
    },
    {
      fieldName: 'sex',
      label: '性别',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.SYSTEM_USER_SEX, 'number'),
        placeholder: '请选择性别',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: '请选择状态',
      },
    },
    {
      fieldName: 'ethnicity',
      label: '民族',
      component: 'Select',
      componentProps: {
        allowClear: true,
        placeholder: '请输入民族',
        options: getDictOptions(DICT_TYPE.SYSTEM_NATION, 'string'),
      },
    },
    {
      fieldName: 'mobile',
      label: '联系电话',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入联系电话',
      },
    },
    {
      fieldName: 'politicalStatus',
      label: '政治面貌',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.SYSTEM_POLITICALINFO, 'string'),
        placeholder: '请选择政治面貌',
      },
    },
    {
      fieldName: 'highestEducation',
      label: '最高学历',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入最高学历',
      },
    },
    {
      fieldName: 'fileNumber',
      label: '档案号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入档案号',
      },
    },
    {
      fieldName: 'staffType',
      label: '编制类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.PER_STAFF_TYPE, 'string'),
        placeholder: '请选择编制类型',
      },
    },
    {
      fieldName: 'teacherCertificate',
      label: '教师资格证号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入教师资格证号',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<PerEmployeeApi.Employee>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '人员ID',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'fullName',
      title: '姓名',
      minWidth: 100,
    },
    {
      field: 'cardType',
      title: '证件类型',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.SYSTEM_CARD_TYPE },
      },
    },
    {
      field: 'idcard',
      title: '身份证号',
      minWidth: 150,
    },
    {
      field: 'birthDate',
      title: '出生年月',
      minWidth: 120,
      formatter: 'formatDate',
    },
    {
      field: 'sex',
      title: '性别',
      minWidth: 80,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.SYSTEM_USER_SEX },
      },
    },
    {
      field: 'ethnicity',
      title: '民族',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.SYSTEM_NATION },
      },
    },
    {
      field: 'mobile',
      title: '联系电话',
      minWidth: 120,
    },
    {
      field: 'politicalStatus',
      title: '政治面貌',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.SYSTEM_POLITICALINFO },
      },
    },
    {
      field: 'highestEducation',
      title: '最高学历',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.SYSTEM_CARD_TYPE },
      },
    },
    {
      field: 'fileNumber',
      title: '档案号',
      minWidth: 120,
    },
    {
      field: 'staffType',
      title: '编制类型',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.PER_STAFF_TYPE },
      },
    },
    {
      field: 'teacherCertificate',
      title: '教师资格证',
      minWidth: 120,
    },
    {
      field: 'auditStatus',
      title: '审核状态',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.PER_AUDIT_STATUS },
      },
    },
    {
      field: 'status',
      title: '人员状态',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
